<?php

namespace App\Domain\Saleman\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Domain\Saleman\Requests\setSalemanPost;
use App\Domain\Saleman\Services\SalemanService;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Pagination\LengthAwarePaginator;

class SalemanController extends Controller
{
    //添加修改
    public function setSaleman(setSalemanPost $request, SalemanService $salemanService)
    {
        $param = $request->validated();
        $data = $salemanService->setSaleman($param);
        return $this->success($data);
    }

    //删除
    public function del(Request $request, SalemanService $salemanService)
    {
        $id = $request['id'];
        $data = $salemanService->del($id);
        return $this->success($data);
    }

    //修改状态
    public function changeState(Request $request, SalemanService $salemanService)
    {
        $res = $salemanService->changeState($request);
        return $this->success($res);
    }

    //列表
    public function list(Request $request, SalemanService $salemanService)
    {
        $this->validate($request, [
            'name' => 'string|nullable',
            'sex' => 'string|nullable',
            'identity' => 'string|nullable',
            'birthDay' => 'string|nullable',
            'remark' => 'string|nullable',
            'status' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'name' => $request['name'],
            'sex' => $request['sex'],
            'identity' => $request['identity'],
            'birthDay' => $request['birthDay'],
            'remark' => $request['remark'],
            'status' => $request['status'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];

        $salemanPaginator = $salemanService->list($param);
        $items = array_map(function ($val) {
            if ($val['sex'] == 1) {
                $val['sexText'] = '男';
            } else {
                $val['sexText'] = '女';
            }
            $val['id'] = strval($val['id']);
            return $val;
        }, $salemanPaginator->items());
        $salemanPaginator = new LengthAwarePaginator($items, $salemanPaginator->total(), $salemanPaginator->perPage(), $salemanPaginator->currentPage());

        //格式化
        $keyMap = [
            'id' => 'id',
            'name' => 'name',
            'sex' => 'sex',
            'sexText' => 'sexText',
            'identity' => 'identity',
            'birthDay' => 'birthDay',
            'remark' => 'remark',
            'status' => 'status',
            'create_time' => function($data){
                return Carbon::parse($data["create_time"])->toDateTimeString();
            },
        ];
        return $this->success(ResponseFormatter::formatterPaginator($salemanPaginator, $keyMap));
    }

    //全部
    public function all(SalemanService $salemanService)
    {
        $data = $salemanService->findAll();
        foreach ($data as $v){
            $v['id'] = strval($v['id']);
        }

        return $this->success($data);
    }
}
